class BugsController < ApplicationController
  layout "main"
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @bug_pages, @bugs = paginate :bugs, :per_page => 10
  end

  def show
    @bug = Bug.find(params[:id])
  end
  
  def assign
    @bug = Bug.find(params[:bug_id])
    @user = User.find(params[:user_id])
    
    #update user_id in bugs
    @bug.user_id = @user.id
    @bug.save
    #add record to bugs_history
    @bug_history = BugHistory.new
    @bug_history.bug_id = @bug.id
    @bug_history.user_id = @user.id
    @bug_history.save
    
    redirect_to :action => 'show', :id => @bug.id   
  end  

  def new
    @bug = Bug.new    
  end

  def create
    @bug = Bug.new(params[:bug])
    if @bug.save
      flash[:notice] = 'Bug was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @bug = Bug.find(params[:id])
  end

  def update
    @bug = Bug.find(params[:id])
    if @bug.update_attributes(params[:bug])
      flash[:notice] = 'Bug was successfully updated.'
      redirect_to :action => 'show', :id => @bug
    else
      render :action => 'edit'
    end
  end

  def destroy
    Bug.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def add_comment
    if params[:comment]
      comment = Comment.new(params[:comment])
      comment.user_id=@current_user.id
      comment.save
    end
    @bug = Bug.find(params[:comment][:bug_id].to_i)
    render :partial => "comments", :locals => {:name=> "mx"}
  end
end
